<template>
  <div class="container">
    <div class="content-wrapper">
      <div class="search-bar">
        <div><el-button type="primary" @click="showAddOrEditPage(false, '', '', 'add', planId)">新增计件</el-button></div>
        <div @click="openFilterDialog" :style="{color: hasFilter ? '#409eff' : '#303133' }">筛选</div>
      </div>
      <div class="box-card-wrapper" ref="scrollcontainer" v-infinite-scroll="loadData" infinite-scroll-disabled="disabled" infinite-scroll-immediate="false">
        <el-card class="box-card" v-for="item in tableData" :key="item.id">
          <div style="padding: 20px;" @click="showMenu(item)">
            <div class="rows">
              <span class="title">{{item.warehousingProductId}}<span v-if="item.warehousingProductName">：{{item.warehousingProductName}}</span></span>
            </div>
            <div class="rows">
              <span><span class="item-name">实际入库数量：</span>{{item.warehousingRealNumber}}</span>
              <span><span class="item-name">发起人：</span>{{item.warehousingCreatorName}}</span>
            </div>
            <div class="rows">
              <span><span class="item-name">单据状态：</span>{{item.warehousingStatus === '' ? warehousingStatus[0] : warehousingStatus[Number(item.warehousingStatus)]}}</span>
              <span><span class="item-name">处理人：</span><span>{{(item.warehousingStatus === '0' || item.warehousingStatus === '') ? '' : item.warehousingStatus === '1' ? item.warehousingQualityAuditName : item.warehousingStatus === '2' ? item.warehousingSignForName : item.warehousingInputName}}</span></span>
            </div>
            <div class="rows">
              <span><span class="item-name">创建日期：</span>{{item.warehousingModifyTime}}</span>
            </div>
          </div>
        </el-card>
        <p v-if="loading" class="tips">加载中...</p>
        <p v-if="noMore" class="tips">没有更多了</p>
      </div>
    </div>
    <FilterPage title="筛选" page="storageUnderPlan" @filterData="filterData" ref="filterpage" bottomHeight='0' />
  </div>
</template>

<script>
import dingMixin from  '@/mixins/ding'
import formUtil from "@/utils/formUtil";
import Tab from '../component/Tab'
import FilterPage from '../component/FilterPage'
export default {
  name: '',
  data () {
    return {
      currentScrollTop: 0,
      tableData: [],
      page: 1,
      totalPage: 1,
      pageSize: 10,
      isFirstLoading: true,
      loading: false,
      warehousingStatus: ['计件单待确认', '待品质审批', '待签收', '待erp录入', 'erp录入完成'],
      currentTypeName: '',
      warehousingBelongId: '',
      menuList: [],
      postForm: {
        planNumber: '',
        startTime: '',
        endTime: '',
      },
      planId: ''
    };
  },
  components: {
    Tab, FilterPage
  },
  mixins: [dingMixin],
  computed: {
    disabled () {
      return this.isFirstLoading || this.loading || this.noMore
    },
    noMore () {
      return this.page >= this.totalPage;
    },
    hasFilter() {
      return this.postForm.startTime !== '' || this.postForm.endTime !== '' || this.postForm.planNumber !== ''
    }
  },
  beforeRouteLeave(to,from,next) {
    this.currentScrollTop = this.$refs.scrollcontainer.scrollTop;
    this.mobileStopRefresh()
    next()
  },
  activated() {
    this.mobileGetDevice(({model, brand, version}) => {
      if(brand === 'iPhone') {
        this.mobildPullToRefresh(() => {
          this.getWarehousingList(true)
        });
      }
    })
    if(window.NEEDREFRESH3 || this.$route.query.planNumber) {
      if(this.$route.query.planNumber) this.planId = this.$route.query.planNumber
      this.getWarehousingList(true);
      window.NEEDREFRESH3 = false;
    } else {
      this.$refs.scrollcontainer.scrollTop = this.currentScrollTop;
    }
  },
  mounted () {
    if(this.$route.query.planNumber) this.planId = this.$route.query.planNumber
  },
  methods: {
    loadData() {
      this.loading = true;
      this.page ++;
      this.getWarehousingList();
    },
    async getWarehousingList(init) {
      if(init) {
        this.page = 1;
        this.tableData = [];
        this.isFirstLoading = true;
      }
      let {list, totalPage} = await this.$httpN.postB('pc/warehousing/list',{
        page: this.page,
        limit: this.pageSize,
        fuzzyPlanNum: this.postForm.planNumber,
        startTime: this.postForm.startTime,
        endTime: this.postForm.endTime,
        warehousingPlanId: this.planId,
      });
      this.loading = false;
      this.tableData.push(...list)
      if(init) {
        this.totalPage = totalPage;
        this.$nextTick(() => {
          this.isFirstLoading = false;
        })
      }
    },
    openFilterDialog() {
      this.$refs.filterpage.init();
    },
    async filterData(param) {
      this.postForm = formUtil.deepCopy(param);
      await this.getWarehousingList(true);
    },
    showAddOrEditPage(id, warehousingBelongId, warehousingBelongName, operation, planNumber) {
      let defalutWarehousingBelongId = warehousingBelongId ? warehousingBelongId : this.$route.query.planBelongId ? this.$route.query.planBelongId : ''
      let defalutWarehousingBelongName = warehousingBelongName ? warehousingBelongName : this.$route.query.planBelongName ? this.$route.query.planBelongName : ''
      this.$router.push({ name: 'mpiecework-storageAddOrEdit', params: {id: id, warehousingBelongId: defalutWarehousingBelongId, warehousingBelongName: defalutWarehousingBelongName, operation: operation, planNumber: planNumber}})
    },
    showMenu(item) {
      let warehousingBelongId = item.warehousingBelongId ? item.warehousingBelongId : this.$route.query.planBelongId ? this.$route.query.planBelongId : ''
      let warehousingBelongName = item.warehousingBelongName ? item.warehousingBelongName : this.$route.query.planBelongName ? this.$route.query.planBelongName : ''
      this.mobileSelect('更多操作',['编辑', '计件单确认','审核', '查看', '删除'], ({buttonIndex}) =>{
        if(buttonIndex === 0) {
          if(item.canModifyData !== '1' ||  item.warehousingStatus !== '1') {
            return this.mobileAlert('你无法编辑')
          }
          this.$router.push({ name: 'mpiecework-storageAddOrEdit', params: {id: item.id, warehousingBelongId: warehousingBelongId, warehousingBelongName: warehousingBelongName, operation: 'edit', planNumber: ''}})
        }else if(buttonIndex === 1) {
          if(!((!item.warehousingCreatorId || (item.warehousingCreatorId && item.warehousingCreatorId === this.userInfo.dingId)) && (item.warehousingStatus==='0' || item.warehousingStatus===''))) {
            return this.mobileAlert('你无法操作')
          }
          this.$router.push({ name: 'mpiecework-storageAddOrEdit', params: {id: item.id, warehousingBelongId: warehousingBelongId, warehousingBelongName: warehousingBelongName, operation: 'edit', planNumber: ''}})
        }else if(buttonIndex === 2) {
          if(!((item.warehousingStatus === '1' && this.userInfo.dingId === item.warehousingQualityAuditId) || 
            (item.warehousingStatus === '2' && this.userInfo.dingId === item.warehousingSignForId) || 
            (item.warehousingStatus === '3' && this.userInfo.dingId === item.warehousingInputId))) {
            return this.mobileAlert('你无法审核');
          }
          this.$router.push({ name: 'mpiecework-storageAddOrEdit', params: {id: item.id, warehousingBelongId: warehousingBelongId, warehousingBelongName: warehousingBelongName, operation: 'check', planNumber: ''}})
        }else if(buttonIndex === 3) {
          this.$router.push({ name: 'mpiecework-storageAddOrEdit', params: {id: item.id, warehousingBelongId: warehousingBelongId, warehousingBelongName: warehousingBelongName, operation: 'view', planNumber: ''}})
        }else if(buttonIndex === 4) {
          if(item.canDeleteData !== '1') return this.mobileAlert('你无法删除')
          this.delData(item)
        }
      })
    },
    async delData(data) {
      formUtil.submitConfirm('请问您是否确定删除这条记录？', async ()=> {
        if(!await this.$httpN.postB(!this.isTime ? 'pc/warehousing/delete' : 'pc/subsidy/delete', {ids: data.id, warehousingBelongName: data.warehousingBelongName})) return;
        formUtil.submitSuccess('删除成功');
        this.getWarehousingList(true);
      })
    }
  }
}
</script>

<style lang='scss' scoped>
.container {
  height: 100vh;
  background-color: #F8F8F8;
  .content-wrapper {
    height: 100%;
    padding: 0 18px 10px;
    .search-bar {
      display: flex;
      justify-content: space-between;
      height: 50px;
      line-height: 50px;
    }
    .box-card-wrapper {
      height: calc(100% - 50px);
      overflow-y: auto;
      .box-card {
        margin-bottom: 10px;
        line-height: 16px;
        &:last-child {
          margin-bottom: 0;
        }
        /deep/ .el-card__body {
          padding: 0;
        }
        .rows {
          margin-bottom: 12px;
          font-size: 14px;
          color: #303334;
          display: flex;
          justify-content: space-between;
          &:last-child {
            margin-bottom: 0;
          }
          .item-name {
            color: #AEAEB0;
          }
        }
        .title {
          font-weight: 700;
          font-size: 16px;
        }
      }
      .tips {
        text-align: center;
        color: #909399;
      }
    }
  }
}
</style>